package  com.nchuoj.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feign.pojo.JudgeTestCase;
import com.nchuoj.answer.mapper.AnswerTestCaseMapper;
import com.nchuoj.answer.pojo.AnswerTestCase;
import com.nchuoj.answer.service.AnswerTestCaseService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author lenovo
* @description 针对表【answer_test_case】的数据库操作Service实现
* @createDate 2022-12-24 18:22:14
*/
@Service
public class AnswerTestCaseServiceImpl extends ServiceImpl<AnswerTestCaseMapper, AnswerTestCase>
    implements AnswerTestCaseService {

    /***
     * 创建一条测试用例运行记录
     * @param answerQuestionId 答题（问题）表记录id 外键
     * @param runTime 运行时间
     * @param memory 所占用内存
     * @param score 测试用例的得分
     * @return 记录是否创建成功
     */
    @Override
    public boolean record(Integer answerQuestionId, Integer runTime, Integer memory, Integer score, String message) {
        AnswerTestCase atc = new AnswerTestCase();
        atc.setMemory(memory);
        atc.setMessage(message);
        atc.setScore(score);
        atc.setRunTime(runTime);
        atc.setAnswerQuestionId(answerQuestionId);
        return this.saveOrUpdate(atc);
    }

    /***
     * 创建一条测试用例运行记录
     * @param list 测试用例数组
     * @param answerQuestionId 答题记录id
     * @return 记录是否创建成功
     */
    @Override
    public List<AnswerTestCase> record(Integer answerQuestionId, List<JudgeTestCase> list) {
        List<AnswerTestCase> answerTestCases = list.stream().map(e -> {
            AnswerTestCase atc = new AnswerTestCase();
            atc.setAnswerQuestionId(answerQuestionId);
            atc.setScore(e.getScore());
            atc.setRunTime(e.getRunTime());
            atc.setMessage(e.getMessage());
            atc.setMemory(e.getMemory());
            atc.setDeleted(0);
            atc.setCreateTime(LocalDateTime.now());
            atc.setUpdateTime(LocalDateTime.now());
            return atc;
        }).toList();
       if(this.saveBatch(answerTestCases)) {
           LambdaQueryWrapper<AnswerTestCase> queryWrapper = new LambdaQueryWrapper<>();
           queryWrapper.eq(AnswerTestCase::getAnswerQuestionId,answerQuestionId);
           return this.list(queryWrapper);
       }
       else {
           return new ArrayList<>();
       }
    }
}




